package org.vectrics.recruiter.struts.tag;

/**
 * @author Mike Moore
 */
import org.apache.log4j.*;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobProvider;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;



public class WorkScheduleComboTag extends TagSupport {
    static private Category log = Category.getInstance(WorkScheduleComboTag.class);
    private String styleClass = null;
    private String property = null;
    private String jobId = null;
    
    public WorkScheduleComboTag() {
    	super();
    }



	public int doStartTag() throws JspException  {
		try {
			StringBuffer html = new StringBuffer();
			
			log.debug("doStartTag() called");
			HttpServletRequest request = (HttpServletRequest)super.pageContext.getRequest();
			String contextPath = request.getContextPath();
			
			if (property == null) {
				throw new JspException("Property attribute not specified.");
			}
			String propValue = request.getParameter(property);
			log.debug("doStartTag() - property = " + property);
			Job job = null;
			
			if (jobId != null) {
				job = RecruitServiceLocator.getJobProvider().findJob(new Long(jobId));
			}
			
			if (job == null) {
				log.debug("Could not find job with id");
			}
	
			//HttpSession session = super.pageContext.getSession();
			if (styleClass == null) {
				html.append("<select name=\"" + property + "\" style=\"width:250;font-family:verdana,arial,sans serif;font-size=" +
						"11px\">");
			}
			else {
				html.append("<select name=\"" + property + "\" class=\"" + styleClass + "\">");
			}
			
			String selectStr = "";
	
			// Full-time option
			if (job != null) {
				if (job.getWorkSchedule() == null)
					job.setWorkSchedule(Job.SCHEDULE_FULLTIME);
				if (job.getWorkSchedule().equals(Job.SCHEDULE_FULLTIME)) {
					selectStr = "selected";
				}
				else {
					selectStr = "";
				}
			}
			html.append("<option value=\"" + Job.SCHEDULE_FULLTIME + "\"" + selectStr + ">Full-Time</option>");
	
	
			// Part time option		
			if (job != null) {		
				if (job.getWorkSchedule().equals(Job.SCHEDULE_PARTTIME)) {
					selectStr = "selected";
				}
				else {
					selectStr = "";
				}
			}
			html.append("<option value=\"" + Job.SCHEDULE_PARTTIME + "\"" + selectStr + ">Part-Time</option>");
	
			// Any option
			if (job != null) {		
				if (job.getWorkSchedule().equals(Job.SCHEDULE_ANY)) {
					selectStr = "selected";
				}
				else {
					selectStr = "";
				}
			}
			html.append("<option value=\"" + Job.SCHEDULE_ANY + "\"" + selectStr + ">Any</option>");
			
			
			
			html.append("</select>");
			
			JspWriter writer = pageContext.getOut();
			try {
				writer.print(html.toString());
			}
			catch (IOException ex) {
				throw new JspException("IOException attempting to write tag to output. " + ex.getMessage());
			}
			log.debug("doStart() finished");
		}
		catch (Exception ex) {
			log.error("Exception displaying work schedule combo", ex);
		}
		return(EVAL_PAGE);
	}


	/**
	 * @return
	 */
	public String getStyleClass() {
		return styleClass;
	}

	/**
	 * @param styleClass
	 */
	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	/**
	 * @return
	 */
	public String getProperty() {
		return property;
	}

	/**
	 * @param property
	 */
	public void setProperty(String property) {
		this.property = property;
	}

	/**
	 * @return
	 */
	public String getJobId() {
		return jobId;
	}

	/**
	 * @param jobId
	 */
	public void setJobId(String jobId) {
		this.jobId = jobId;
	}

}
